<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}
if($_POST['sub']!=''){
    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
    
    if(empty($_POST["shop_id"])){
       jumpajax('300','名厨(名店)不能为空!',''); 
    }
    if(trim($_POST["title"])==""){
        
        jumpajax('300','套餐名称不能为空!','');
    }
    if(trim($_POST["rens"])==""){
        
        jumpajax('300','适用人数不能为空!','');
    }
    if(trim($_POST["marketprice"])==""){
        
        jumpajax('300','套餐总价不能为空!','');
    }
    
    if(trim($_POST["totalprice"])==""){
        
        jumpajax('300','预订价格不能为空!','');
    }    
    
    
    $status=intval($_REQUEST["status"]);
    $p=0;
    $imglist=array();
    
    
    //
    
	$data=array('title'=>trim($_POST["title"]),
    'nums'=>0,
    'rens'=>intval($_POST["rens"]),
    'totalprice'=>$_POST["totalprice"],
    'marketprice'=>$_POST["marketprice"],
    'thumb'=>trim($_POST["imgs0_fileName"]),
    'description'=>trim($_POST["description"]),
    'displayorder'=>intval($_POST["listorder"]),
    'createtime'=>time(),
    'content'=>trim($_POST["content"]),
    'status'=>$status,
    'weid'=>$_SESSION['EZ_Admin']['id'],
    'shop_id'=>trim($_POST["shop_id"])
    
    );
    $i=1;
    $metlist=array();
    $myrow=array();
    $goodslist=$_REQUEST["goods"];
    $productprice=0;
    while(list($k,$v)=each($goodslist)){
        
        if(!empty($v)){
            $finfo=$ezhand->select("shopping3_goods","*","id=".$v);
            $myrow["id"][]=$v;
            $myrow["title"][]=$finfo['title'];
            $productprice=$productprice+$finfo["productprice"];
            $metlist[]=$v;
    
        }
        //if(!empty($myrow["id"])){
        //$metlist[]=$myrow;
        //}
        $i++;    
    }
    if(count($metlist)<1){
        
        jumpajax('300','菜品至少选择一项!','');
    }
    //$productprice
    
    $dishes=serialize($myrow);
    $data["dishes"]=$dishes;
    $data["productprice"]=$productprice;
    $data["nums"]=count($metlist);

    $goods=array(
        'title'=>trim($_POST["title"]),
        'productprice'=>trim($_POST["marketprice"]),
        'totalprice'=>$_POST["totalprice"],
        'marketprice'=>$_POST["marketprice"],
        'thumb'=>trim($_POST["imgs0_fileName"]),
        'thumb_url'=>trim($_POST["imgs0_fileName"]),
        'displayorder'=>intval($_POST["listorder"]),
        'isindex'=>intval($_POST["isindex"]),
        'createtime'=>time(),
        'productsn'=>'',
        'content'=>trim($_POST["content"]),
        'status'=>$status,
        'weid'=>$_SESSION['EZ_Admin']['id'],
        'pcate'=>6,
        "title_en"=>mysql_escape_string($_POST["title_en"]),//没有
        "shop_id" => trim($_POST["shop_id"]),
    );
 	if($id==0){
        $id=$ezhand->insert("shopping3_genius",$data);
        //同时插入菜品表
        //$goods['tid'] = $id;
        //$gid=$ezhand->insert("shopping3_goods",$goods);
        
        //$gdata=array("goodsid"=>$gid);
        //$ezhand->update("shopping3_genius",$gdata,'id="'.$id.'"');
        
		jumpajax('200','套餐保存成功','page435','closeCurrent');
	}else{
	   // $data["goodsid"]=$ezhand->select("shopping3_goods","id","tid=".$id);
        
	    $ezhand->update("shopping3_genius",$data,'id="'.$id.'"');
        //同时修改菜品t
        //$ezhand->update("shopping3_goods",$goods,'tid="'.$id.'"');
		jumpajax('200','套餐修改成功','page435','closeCurrent');
	}
}else{
    $chefid=intval($_REQUEST["chefid"]);
    if(empty($chefid)){
        $cheflisttemp=$ezhand->get_chefbyid("shopping3_chef");
        $cheflist=array("0"=>"请选择");        
    }else{
        $cheflisttemp=$ezhand->select_all("shopping3_chef",'*','id="'.$chefid.'"');
        
        $cailist=$ezhand->select_all("shopping3_goods","*","shop_id=".$chefid." order by pcate");
        
    }
   
    
    foreach($cheflisttemp as $val){
        $sid=$val["id"];
        $cheflist[$sid]=$val["name"];  

    }
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
    if($id==0){
	    $row=array();
        $dishes=array();
	}else{
	   
	    $row=$ezhand->select("shopping3_genius",'*','id="'.$id.'"');
        if($row["dishes"]<>""){
            $dishes=unserialize($row["dishes"]);
            
            $dishesidlist=$dishes["id"];
        }else{
            $dishes=array();
        }
	}
}
?>